package smartcrud.spring.controllers.crud;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import smartcrud.common.orm.Page;
import smartcrud.common.orm.PropertyFilter;
import smartcrud.common.orm.jdbc.JdbcUtils;
import smartcrud.spring.models.crud.Hello;
import smartcrud.spring.models.crud.HelloService;

/**
 * Rails 1.2中对REST的实现默认把controller中的方法限制在7个：<br>
 * index、show、new、edit、create、 update和destroy.<br>
 * 而根据SpringSide4,使用Restful风格的Urls:<br>
 * 
 * <pre>
 * List page     : GET  /hello/ 
 * Show page     : GET  /hello/{id}
 * Create page   : GET  /hello/create
 * Create action : POST /hello/create
 * Update page   : GET  /hello/update/{id}
 * Update action : POST /hello/update
 * Delete action : GET  /hello/delete
 * </pre>
 * 
 * @author cyper.yin
 * 
 */
@Controller
@RequestMapping(value = "/hello")
public class HelloController {
	private static final String SUCCESS = "redirect:/hello/";

	@Autowired
	private HelloService helloService;

	@RequestMapping(value = "")
	public String index(
			@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
			Model model, HttpServletRequest request) {

		List<PropertyFilter> filters = JdbcUtils.buildPropertyFilters(request);

		Page<Hello> page = new Page<Hello>(10);
		page.setPageIndex(pageIndex);
		if (!page.isOrderBySetted()) {
			page.setOrderBy("create_date");
			page.setOrder("desc");
		}
		page = helloService.findByPage(page, filters);
		model.addAttribute("page", page);

		return "hello/hello_index";
	}

	@RequestMapping("/{id}")
	public ModelAndView show(@PathVariable("id") Long id) {
		Hello hello = helloService.findHelloById(id);
		ModelAndView mv = new ModelAndView("hello/hello_show");
		mv.addObject(hello);

		return mv;
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(ModelMap model) {
		Hello hello = new Hello();
		model.addAttribute(hello);
		return "hello/hello_new";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@ModelAttribute("hello") Hello hello,
			RedirectAttributes redirectAttributes) {
		helloService.createHello(hello);
		redirectAttributes.addFlashAttribute("message", "create success!");
		return SUCCESS;
	}

	@InitBinder
	protected void initBinder(WebDataBinder binder) throws ServletException {
		binder.registerCustomEditor(byte[].class,
				new ByteArrayMultipartFileEditor());
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public ModelAndView updateForm(@PathVariable("id") Long id) {
		Hello hello = helloService.findHelloById(id);
		ModelAndView mv = new ModelAndView("hello/hello_edit");
		mv.addObject(hello);
		return mv;
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@RequestParam("id") Long id,
			@RequestParam("name") String name,
			@RequestParam("age") Integer age,
			RedirectAttributes redirectAttributes) {
		Hello hello = helloService.findHelloById(id);
		hello.setName(name);
		hello.setAge(age);
		helloService.updateHello(hello);
		redirectAttributes.addFlashAttribute("message", "update success!");
		return SUCCESS;
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id,
			RedirectAttributes redirectAttributes) {
		helloService.deleteHello(id);
		redirectAttributes.addFlashAttribute("message", "delete success!");
		return SUCCESS;
	}
}
